package main

import (
	"context"
	"fmt"

	_ "github.com/mbobakov/grpc-consul-resolver" // It's important
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"

	"oldpackagetest/grpc_test/proto"
)

func main() {
	conn, err := grpc.Dial(
		//consul网络必须是通的   user_srv表示服务 wait:超时 tag是consul的tag  可以不填
		"consul://192.168.10.130:8500/user_srv?wait=14s",
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		//轮询法   必须这样写   grpc在向consul发起请求时会遵循轮询法
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		panic(err)
	}
	defer conn.Close()
	//发起10次请求
	for i := 0; i < 10; i++ {
		c := proto.NewGreeterClient(conn)
		////从这开始用自己的protobuf
		r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "bobby"})
		if err != nil {
			panic(err)
		}
		fmt.Println(r.Message)
	}
}
